package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.PotentialStudent;
import cn.wolfcode.crm.query.StudentShiftQueryObject;
import cn.wolfcode.crm.service.IStudentShiftService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("studentShift")
public class StudentShiftController {
    @Autowired
    private IStudentShiftService studentShiftService;


    @RequestMapping("view")
    public String view(){
        return "studentShift";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(StudentShiftQueryObject qo){
        return studentShiftService.queryShiftList(qo);
    }

    @RequestMapping("update")
    public void update(PotentialStudent ps){
        studentShiftService.update(ps);
    }

    @RequestMapping("audit")
    @ResponseBody
    public JSONResult audit(long id){
        JSONResult jsonResult = new JSONResult();
        try {
            studentShiftService.audit(id);
            return jsonResult;
        }catch (Exception e){
            jsonResult.remark(e.getMessage());
            return jsonResult;
        }
    }



    @RequestMapping("export")
    public void export(HttpServletResponse resp) throws IOException {
        //设置下载响应头
        resp.setHeader("Content-Disposition","attachment;filename=spending.xls");
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = wb.createSheet();
        //查询支出数据
        List<PotentialStudent> pss = studentShiftService.All();
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        //创建单元格,设置标题
        row.createCell(0).setCellValue("学生姓名");
        row.createCell(1).setCellValue("转班时间");
        row.createCell(2).setCellValue("学生电话");
        row.createCell(3).setCellValue("之前的班级");
        row.createCell(4).setCellValue("转入的班级");
        row.createCell(5).setCellValue("对应客服");
        row.createCell(6).setCellValue("转班状态");
        row.createCell(7).setCellValue("已缴学费");
        row.createCell(8).setCellValue("待缴学费");
        for (int i = 0;i < pss.size(); i++){
            //从第二行开始再创建行
            row = sheet.createRow(i + 1);
            PotentialStudent ps = pss.get(i);
            //创建单元格,填充内容
            row.createCell(0).setCellValue(ps.getName());
            Date time = ps.getShiftTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(time);
            row.createCell(1).setCellValue(date);
            row.createCell(2).setCellValue(ps.getTel());
            row.createCell(3).setCellValue(ps.getGrade().getClassName());
            row.createCell(4).setCellValue(ps.getNewClassName());
            row.createCell(5).setCellValue(ps.getSalesman().getUsername());
            if (ps.isShiftAudit()) {
                row.createCell(6).setCellValue("已审核");
            }else {
                row.createCell(6).setCellValue("未审核");
            }
            BigDecimal money = ps.getMoney();
            row.createCell(7).setCellValue(money.toPlainString());
            BigDecimal owe = ps.getOwe();
            row.createCell(8).setCellValue(owe.toPlainString());
        }
        //输出到浏览器
        wb.write(resp.getOutputStream());
    }
}
